package com.sllp.C71S3TsllpShop.web.back;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.sllp.C71S3TsllpShop.bean.Address;
import com.sllp.C71S3TsllpShop.bean.AddressExample;
import com.sllp.C71S3TsllpShop.bean.Admin;
import com.sllp.C71S3TsllpShop.bean.Business;
import com.sllp.C71S3TsllpShop.bean.BusinessExample;
import com.sllp.C71S3TsllpShop.bean.Result;
import com.sllp.C71S3TsllpShop.bean.User;
import com.sllp.C71S3TsllpShop.dao.AddressMapper;
import com.sllp.C71S3TsllpShop.dao.AdminMapper;
import com.sllp.C71S3TsllpShop.dao.BusinessMapper;

@Controller
public class BackBusinessInfoAction {

	@Resource 
	private BusinessMapper bm ;
	
	@Resource
	private AdminMapper am; 
	
	@Resource
	private AddressMapper adm;
	
	
	@GetMapping("back/toBusinessInfo.do")
	public String toBusinessInfo(@SessionAttribute(name="loginUser",required=false) User user,
			@SessionAttribute(name="backLoginBusiness",required=false) Business business,
			@SessionAttribute(name="backLoginAdmin",required=false) Admin admin,
			Model m) {
		Business business2 =new Business();
		Admin ad =new Admin();
		if(user != null && business != null) {
			
			business2=bm.selectByPrimaryKey(business.getId());
			m.addAttribute("admin", ad);
			m.addAttribute("business", business2);
			m.addAttribute("login", 1);
		
			
		}else if(admin != null ){
			ad=am.selectByPrimaryKey(admin.getId());
			m.addAttribute("login", 2);
			m.addAttribute("business", business2);
			m.addAttribute("admin", ad);
			
		}
		return "back/business-info::businessInfo"; 
	}
	
	@ResponseBody
	@PostMapping("back/addBusinessAddr.do")
	public Result addBusinessAddr(@RequestParam String addr,@SessionAttribute(name="backLoginBusiness",required=false) Business business){
		int ok =0;
		Address address =new Address();
		address.setId(business.getAddr());
		address.setName(addr);
		ok=adm.updateByPrimaryKeySelective(address);
		if(ok<=0) {
			return new Result(ok,"修改失败");
		}
		return new Result(ok,"修改成功");
		
	}
	
	
}
